<template>
  <a-modal
    :title="title"
    :width="1250"
    :visible="visible"
    @cancel="handleCancel"
    :cancelText="$t('systemManagement.close')"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{$t('inPrint.cancel')}}</a-button>
    </template>
    <a-form layout="inline" @keyup.enter.native="searchQuery">
     <a-row>
       <!-- 验收仓库 -->
       <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol"
              :label="$t('purchasingManagement.WarehousingWarehouse')">
              <a-select style="width: 200px" :placeholder="$t('purchasingManagement.selectWarehousingWarehouse')" 
              v-model="queryParam.depotIds">
                <a-select-opt-group>
                  <span slot="label">{{$t('inPrint.self')}}</span>
                  <a-select-option v-for="(item, index) in selfDepot" :key="index" :value="item.id">
                  {{ item.depotName || item.name }}
                </a-select-option>
                </a-select-opt-group>
                <a-select-opt-group :label="$t('inPrint.other')">
                  <a-select-option v-for="(item, index) in othoerDepot" :key="index" :value="item.id">
                  {{ item.depotName || item.name }}
                </a-select-option>
                </a-select-opt-group>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :sm="24">
            <a-form-item>
                <a-button type="primary" @click="searchQuery">{{$t('manuManagement.query')}}</a-button>
                <a-button v-if="btnEnableList.indexOf(5) > -1" style="margin-left: 8px" v-print="'#reportPrintDetail'" icon="printer">{{ $t('reportQuery.print') }}</a-button>
                <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="generatePrintData($event,'SPKC-detail.fr3')" icon="printer">{{ $t('inPrint.other') + $t('reportQuery.print') }}</a-button>
                <a-button v-if="btnEnableList.indexOf(3) > -1" :loading="downLoading" style="margin-left: 8px" @click="exportExcel" icon="download">{{ $t('reportQuery.export') }}</a-button>
              </a-form-item>
            
          </a-col> 
     </a-row>
    </a-form>
          
    <!-- table区域-begin -->
    <a-row style="margin-bottom:20px">
       
    </a-row>   
    <section id="reportPrintDetail">     
    <a-table
      bordered
      ref="table"
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange">
      <span slot="numberCustomRender" slot-scope="text, record">
        <a @click="myHandleDetail(record)">{{record.number}}</a>
      </span>
    </a-table>
  </section>  
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <bill-detail ref="billDetail"></bill-detail>
  </a-modal>
</template>
<script>
  import BillDetail from '../../bill/dialog/BillDetail'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import { findBillDetailByNumber, findFinancialDetailByNumber } from '@/api/api'
  import { enumType,titleObj } from '@/lang/category';
  import { getAction } from '@/api/manage'
  import { getLanCode } from '@/lang';
  export default {
    name: "MaterialInOutList",
    mixins:[JeecgListMixin],
    components: {
      BillDetail,
      JEllipsis
    },
    data () {
      return {
        title:this.$t('setailManagement.operation'),
        visible: false,
        disableMixinCreated: false,
        toFromType: '',
        btnEnableList:'',
        selfDepot:[],
        othoerDepot:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        // 查询条件
        queryParam: {
          depotIds: '',
          materialId:'',
        },
        tabKey: "1",
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:40,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: this.$t('setailManagement.documentNo'), dataIndex: 'number', width: 100,
            scopedSlots: { customRender: 'numberCustomRender' },
          },
          {
            title: this.$t('basicInformation.type'), dataIndex: 'type', width: 100,customRender: (t, r, index) => {
              if(getLanCode() === 'en'){
                return enumType[t] === undefined ? t : enumType[t]
              }else{
                return t
              }
            }
          },
          // { title: this.$t('备注'), dataIndex: 'type', width: 100},
          { title: this.$t('reportQuery.barcode'), dataIndex: 'barCode', width: 100},
          { title: this.$t('reportQuery.name'), dataIndex: 'materialName', width: 200},
          { title: this.$t('reportQuery.category'), dataIndex: 'categoryName',width:80 },
          { title: this.$t('basicInformation.warehouseName'), dataIndex: 'depotName', width: 80},
          { title: this.$t('reportQuery.quantity'), dataIndex: 'basicNumber', sorter: (a, b) => a.basicNumber - b.basicNumber, width: 60},
          { title: this.$t('inPrint.date'), dataIndex: 'operTime', width: 70}, 
          { title: this.$t('manuManagement.breakageType'), dataIndex: 'custom11', width: 80,customRender: (t,r,index) => {
            if(+t === 0){
              return this.$t('manuManagement.breakageTypeNormal')
            }else if(+t === 1){
              return this.$t('manuManagement.breakageTypeBreaked')
            }else if(+t === 2){
              return this.$t('manuManagement.breakageTypeExchanged')
            }
          }},
          { title: this.$t('manuManagement.breakageNum'), dataIndex: 'differNumber', sorter: (a, b) => a.differNumber - b.differNumber, width: 80},
        ],

        url: {
          list: "/depotItem/findDetailByDepotIdsAndMaterialId"
        }
      }
    },
    created() {
      this.getAnotherDepotData()
    },
    methods: {
      getQueryParams() {
        let param = Object.assign({}, this.queryParam, this.isorter);
        param.field = this.getQueryField();
        param.currentPage = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        return param;
      },
      show(record, depotIds,btnEnableList) {
        this.model = Object.assign({}, record);
        console.log('999 :>> ', btnEnableList);
        this.visible = true;
        this.queryParam.depotIds = depotIds
        this.queryParam.materialId = record.id
        this.btnEnableList = btnEnableList
        this.loadData(1)
      },
      
    /**
     * 区分本租户的仓库和别人的仓库
     */
    getAnotherDepotData() {
      getAction('/depot/findDepotList').then(res => {
        if (res.code === 200) {
          this.anotherDepotList = res.data
          let creator = this.getLocalStorage('pro__Login_Userinfo').tenantId
          this.selfDepot = this.anotherDepotList.filter(v=>v.tenantId === creator)
          this.othoerDepot = this.anotherDepotList.filter(v=>v.tenantId !== creator)
        } else {
          this.$message.info(res.data)
        }
      })
    },
    
    // 获取localStorage数据
    getLocalStorage (key) {
      var value = localStorage.getItem(key) // 取回 value 变量
      this.localStorageKeyJson = JSON.parse(value)
      return this.localStorageKeyJson.value
    },
      reportShow(record,query,type){
        this.model = Object.assign({}, record,query);
        this.visible = true;
        this.queryParam.beginTime = query.beginTime
        this.queryParam.endTime = query.endTime
        this.queryParam.materialId = record.materialId
        this.queryParam.type1 ='其它' 
        this.queryParam.subType1 =  type == 1 ? '收费确认' :'直发采购退货'
        if(type === 1){
          this.queryParam.type = '入库'
          this.queryParam.subType = '采购'

        }else{
          
          this.queryParam.type = '出库'
          this.queryParam.subType = '采购退货'
        }
        this.loadData(1)
      },
      setUrl(url){
        for (let item of this.columns) {
          if (item.dataIndex == 'materialName') {
            item.dataIndex = 'name'
          }
          if (item.dataIndex == 'basicNumber') {
            item.dataIndex = 'operNumber'
          }
        }
        this.$set(this.url,'list',url)
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      myHandleDetail(record) {
        let that = this
        this.toFromType = record.fromType
        findBillDetailByNumber({ number: record.number }).then((res) => {
          if (res && res.code === 200) {
            that.$refs.billDetail.show(res.data, record.type);
            let title = record.type
            if(getLanCode() === 'en'){
              title = titleObj[title] || title
            }
            that.$refs.billDetail.title = title + '-' + this.$t('reportQuery.details');
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>